package Bombe.Test;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import error.InvariantError;
import error.PostconditionError;
import error.PreconditionError;

import Bombe.BombeContrat;
import Bombe.BombeImpl;
import Bombe.BombeService;

public class BombeTest {
	BombeService bombe;

	@Before
	public void before(){
		bombe=new BombeContrat(new BombeImpl());
	}

	@Test
	public void initPre1Test(){
		try{
			bombe.init(1, 1, 1, 5);
		}catch(PreconditionError e){
			System.out.println("Precondition : " + e + " catcher");
			Assert.assertTrue(false);
		}catch(PostconditionError e){
			System.out.println("Postcondition :" + e + "catcher");
			Assert.assertTrue(false);
		}catch(InvariantError e){
			System.out.println("Invariant : " + e + "catcher");
			Assert.assertTrue(false);
		}
	}	

	@Test
	public void initPre2Test(){
		try{
			bombe.init(1, 1, 1, -2);
			Assert.assertTrue("Erreur init(n,x,y,amp) => amp in [3,11]", false);
		}catch(PreconditionError e){
			System.out.println("Precondition : " + e + " catcher");
			Assert.assertTrue(true);
		}catch(PostconditionError e){
			System.out.println("Postcondition :" + e + "catcher");
			Assert.assertTrue(false);
		}catch(InvariantError e){
			System.out.println("Invariant : " + e + "catcher");
			Assert.assertTrue(false);
		}
	}		

	@Test
	public void initPre3Test(){
		try{
			bombe.init(1, 1, 1, 12);
			Assert.assertTrue("Erreur init(n,x,y,amp) => amp in [3,11]",false);
		}catch(PreconditionError e){
			System.out.println("Precondition : " + e + " catcher");
			Assert.assertTrue(true);
		}catch(PostconditionError e){
			System.out.println("Postcondition :" + e + "catcher");
			Assert.assertTrue(false);
		}catch(InvariantError e){
			System.out.println("Invariant : " + e + "catcher");
			Assert.assertTrue(false);
		}
	}

	@Test
	public void initPostTest(){
		try{
			bombe.init(0, 0, 0, 9);
		}catch(PostconditionError p){
			Assert.assertTrue(p.toString(),false);
		}
		//\obj:getNumero(init(num,x,y,amplitude)) = num 
		Assert.assertTrue(bombe.getNumero()==0);
		//\obj: getX(init(num,x,y,amplitude)) = x
		Assert.assertTrue(bombe.getX()==0);
		//\obj: getY(init(num,x,y,amplitude)) = y 
		Assert.assertTrue(bombe.getY()==0);
		//\obj:getAmplitude(init(num,x,y,amplitude)) = amplitude
		Assert.assertTrue(bombe.getAmptitude()==9);
		//\post:getCompteARebours(init(num,x,y,amplitude)) = 10
		Assert.assertTrue(bombe.getCompteARebours()==10);
	}
	
	@Test
	public void diminueChronoPre1Test(){
		try {
			bombe.init(1, 1, 1, 5);
		}catch(Error e){
			Assert.assertTrue(e.toString(), false);
		}
		try{
			bombe.diminueChrono();//9
			bombe.diminueChrono();//8
			bombe.diminueChrono();//7
			bombe.diminueChrono();//6
			bombe.diminueChrono();//5
			bombe.diminueChrono();//4
			bombe.diminueChrono();//3
			bombe.diminueChrono();//2
			bombe.diminueChrono();//1
			bombe.diminueChrono();//0
		}catch(PreconditionError e){
			System.out.println("Precondition : " + e + " catcher");
			Assert.assertTrue(false);
		}catch(PostconditionError e){
			System.out.println("Postcondition :" + e + "catcher");
			Assert.assertTrue(false);
		}catch(InvariantError e){
			System.out.println("Invariant : " + e + "catcher");
			Assert.assertTrue(false);
		}
	}
	
	@Test
	public void diminueChronoPre2Test(){
		try {
			bombe.init(1, 1, 1, 5);
		}catch(Error e){
			Assert.assertTrue(e.toString(), false);
		}
		try{
			bombe.diminueChrono();//9
			bombe.diminueChrono();//8
			bombe.diminueChrono();//7
			bombe.diminueChrono();//6
			bombe.diminueChrono();//5
			bombe.diminueChrono();//4
			bombe.diminueChrono();//3
			bombe.diminueChrono();//2
			bombe.diminueChrono();//1
			bombe.diminueChrono();//0
			bombe.diminueChrono();//-1 Erreur
			Assert.assertTrue("Erreur ne peut diminuerChrono() que si chrono >0",false);
		}catch(PreconditionError e){
			System.out.println("Precondition : " + e + " catcher");
			Assert.assertTrue(true);
		}catch(PostconditionError e){
			System.out.println("Postcondition :" + e + "catcher");
			Assert.assertTrue(false);
		}catch(InvariantError e){
			System.out.println("Invariant : " + e + "catcher");
			Assert.assertTrue(false);
		}
	}

	@Test
	public void diminueChronoPostTest(){
		try{
			bombe.init(0, 0, 0, 9);
		}catch(Error e){
			Assert.assertTrue(e.toString(), false);
		}
		try{
			bombe.diminueChrono();
		}catch(PostconditionError p){
			Assert.assertTrue(false);
			System.err.println(p);
		}
		Assert.assertTrue(bombe.getNumero()==0);
		Assert.assertTrue(bombe.getX()==0);
		Assert.assertTrue(bombe.getY()==0);
		Assert.assertTrue(bombe.getAmptitude()==9);
		Assert.assertTrue(bombe.getCompteARebours()==9);
	}
}
